package com.example.shoppingcart.service.impl;

import com.example.common.utils.JWTUTils;
import com.example.common.utils.UserContext;
import com.example.model.shppingcart.dto.GoodsAndCountDTO;
import com.example.model.shppingcart.dto.ShoppingCartDTO;
import com.example.shoppingcart.mapper.ShoppingCartMapper;
import com.example.shoppingcart.service.ShoppingCartService;
import io.jsonwebtoken.Claims;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceimpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    @Transactional
    public ShoppingCartDTO addCart(GoodsAndCountDTO goodsAndCountDTO) {
        log.info("{}",goodsAndCountDTO);
        //查找关于user cart
        String username = UserContext.getUser();
        ShoppingCartDTO shcrdto=shoppingCartMapper.selectUserCart(username,goodsAndCountDTO.getId());
        log.info("{}shcrdto",shcrdto);
        //cart 中有无 product id
        if (shcrdto==null){
            //没有添加
            ShoppingCartDTO sc= shoppingCartMapper.selectGoods(goodsAndCountDTO);
            sc.setCount(goodsAndCountDTO.getCount());
            sc.setUsername(username);
            log.info("{}sc",sc);
            shoppingCartMapper.insertCart(sc);
        }
        else {
            //有
            //添加数量
            int i = shcrdto.getCount() + goodsAndCountDTO.getCount();
            shoppingCartMapper.updateCart(i,username,goodsAndCountDTO.getId());
        }
        return null;
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCartDTO> selectCart() {
        String username = UserContext.getUser();
        List<ShoppingCartDTO> list = shoppingCartMapper.selectCart(username);
        return list;
    }

    @Override
    public void delectCart(List<Integer> ids) {

    }

}
